package com.zgs.mall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.zgs.mall.order.entity.OrderEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.Query;

import com.zgs.mall.order.dao.OrderItemDao;
import com.zgs.mall.order.entity.OrderItemEntity;
import com.zgs.mall.order.service.OrderItemService;

//@RabbitListener(queues = {"hello-Queue","guli.news"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 参数可以写：
     * Channel 当前传输消息的通道
     * queues：可以很多人监听，只要收到消息，队列删除消息，而且只能一个收到
     *  只有一个消息
     * @param message
     * @param entity 消息类型
     */
  //  @RabbitHandler
    public void rabbitmessage(Message message, OrderEntity entity, Channel channel){
        System.out.println("第一个接收到的消息:"+entity);
        //channel 内 按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //手动确认 非批量模式
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            //不确认消息，拒签
            try {
                channel.basicNack(deliveryTag,false,true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
    }
  //  @RabbitHandler
    public void rabbitmessage2(Message message, String entity, Channel channel){
        System.out.println("第两个接收到的消息:"+entity);
    }
}